import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import Helper from "../libs/HelperMgr";
import Ground from "./Ground";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {

    public static instace: GameManager = null;

    @property(cc.Node)
    player: cc.Node = null;


    @property(cc.Node)
    stoshs: cc.Node = null;

    @property(cc.Node)
    sprays: cc.Node = null;


    @property(cc.Prefab)
    spray: cc.Prefab = null;


    /**玩家所在地面 */
    ground: cc.Node = null;

    /**当前力度百分比 */
    public forcePercent: number = 0;
    /**游戏是否开始 */
    public gameIsStart: boolean = false;
    /**游戏是否结束 */
    public gameIsOver: boolean = false;
    /*玩家是否打到鱼 */
    public playerIsHit: boolean = false;
    /*玩家是否正在挥棒 */
    public playerIsWave: boolean = false;
    /*玩家是否扔出鱼饵 */
    public playerIsStosh: boolean = false;
    /**游戏击打次数 */
    public gameChance: number = 3;
    /**游戏开始时间 */
    public gameStartTime: number = 60;
    /**游戏运行时间 */
    public gameTime: number = 60;


    /**本关游戏得分 */
    public gameScore: number = 0;



    onLoad() {
        GameManager.instace = this;
        //预加载分包资源
        cc.assetManager.loadBundle('animation');
        // console.log("当前关卡",GameData.instance.gameStage)

    }

    start() {
        if (GameData.instance.gameBack) {
            this.goon();
        } else {
            this.init();
        }

    }

    init() {
        this.gameIsStart = true;
        this.gameIsOver = false;
        this.playerIsHit = false;
        this.playerIsWave = false;
        this.playerIsStosh = false;
        this.gameScore = 0;
        this.gameChance = 3;
        this.gameStartTime = new Date().valueOf();
        if (!GameData.instance.gameModel) {
            this.schedule(() => {
                this.gameTime = Math.floor(Constants.CrazyGameTime - (new Date().valueOf() - this.gameStartTime) / 1000);
                if (this.gameTime < 0 && !this.gameIsOver && !this.playerIsHit && !this.playerIsWave) {
                    this.gameOver();
                }
            }, 1)
        }
    }

    goon() {
        GameData.instance.gameBack = false;
        this.gameIsStart = true;
        this.gameIsOver = false;
        this.playerIsHit = false;
        this.playerIsWave = false;
        this.playerIsStosh = false;
        this.gameScore = GameData.instance.gameScore;
        this.gameChance = GameData.instance.gameChance;
    }

    update(dt) {
        if (this.gameIsStart && !this.gameIsOver) {
            GameData.instance.playScore = this.gameScore;
            if (GameData.instance.gameModel && this.gameChance <= 0 && !this.playerIsHit && !this.playerIsWave) {
                this.gameOver();
            }
        }
    }

   

    public createSpray(pos: cc.Vec2) {
        let spray: cc.Node = cc.instantiate(this.spray);
        spray.setParent(this.sprays);
        spray.setPosition(pos)
        cc.tween(spray).to(1, { scaleX: 3, scaleY: 3, opacity: 0 }).call(() => {
            spray.destroy();
        }).start()

    }

    public gameOver() {
        this.gameIsStart = false;
        this.gameIsOver = true;
        Helper.showUI(Constants.UIName.UIOver);
    }


    public backMain() {
        //判断是否是正常游戏模式
        if (GameData.instance.gameModel) {
            //判定是否是中途退出游戏
            if (!this.gameIsOver) {
                //是中途退出的，记录数据
                GameData.instance.gameBack = true;
                GameData.instance.gameScore = this.gameScore;
                GameData.instance.gameChance = this.gameChance;
            } else {
                GameData.instance.gameBack = false;
            }

        }
        cc.director.loadScene("index");
    }
}
